package com.example.udpLearn.udp2;

import java.io.IOException;
import java.net.*;

/**
 * 客户端发送一次数据接收一次数据，服务端接收一次数据发送一次数据：客户端
 */
public class Client {
    public static void main(String[] args) throws IOException {

        System.out.println("客户端启动...");

        DatagramSocket socket = new DatagramSocket(6666);

        /**
         * 客户端发送数据
         */
        byte[] clientSendData = "你好，我是客户端".getBytes();
        DatagramPacket packetSend = new DatagramPacket(clientSendData, clientSendData.length, InetAddress.getLocalHost(), 8888);

        socket.send(packetSend);

        /**
         * 接收服务端数据
         */
        byte[] clientReceiveData = new byte[1024 * 64];
        DatagramPacket packetReceive = new DatagramPacket(clientReceiveData, clientReceiveData.length);

        socket.receive(packetReceive);

        System.out.println("地址：" + packetReceive.getAddress());
        System.out.println("端口：" + packetReceive.getPort());
        System.out.println("服务端发来的数据：" + new String(packetReceive.getData(), 0, packetReceive.getLength()));

        /**
         * 释放资源
         */
        socket.close();

    }
}
